// pages/damage/damage.js
var util = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    number: 1,
    currentWordNumber: 0,
    cartArr: [
      {
        name: '瓶',
        value: 1,
        checked: !0
      },
      {
        name: '箱',
        value: 2
      }
    ],
    type: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOrderDetail(options.id)
  },
  getOrderDetail: function (val) {
    const _this = this
    util.request(getApp().api.goods.orderDetail,{
      orderId: val,
    }).then(
      function(res) {
        if (res.code == 200) {
          _this.setData({
            order_detail: res.data,
          });
        } else {
          wx.showToast({
            title: res.msg, //提示的内容,
            icon: "none", //图标,
            //duration: 1500, //延迟时间,
            mask: false, //显示透明蒙层，防止触摸穿透,
          });
          setTimeout(function() {
            wx.hideToast();
          }, 1500);
          return
        }
      }
    );
  },
  numberSub: function() {
    this.setData({
        number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
    });
  },
  numberAdd: function() {
    if(this.data.type==1){
      this.setData({
          number: this.data.number + 1 >= this.data.order_detail.productNum*24 ? this.data.order_detail.productNum*24 : this.data.number + 1
      });
    }
    if(this.data.type==2){
      this.setData({
          number: this.data.number + 1 >= this.data.order_detail.productNum ? this.data.order_detail.productNum : this.data.number + 1
      });
    }
  },
  numberBlur: function(e) {
    var num = ~~e.detail.value;
    if (num <= 0) num = 1;
    if(this.data.type==1){
      if (num > this.data.order_detail.productNum*24) num = this.data.order_detail.productNum*24;
      this.setData({ number: num});
    }
    if(this.data.type==2){
      if (num > this.data.order_detail.productNum) num = this.data.order_detail.productNum;
      this.setData({ number: num});
    }
  },
  radioChange: function(val){
    this.setData({
      type: val.detail.value
    });
    if(val.detail.value==2&&this.data.number>this.data.order_detail.productNum){
      this.setData({
        number: this.data.order_detail.productNum
      });
    }
  },
  getValueLength: function (e) {
    let value = e.detail.value
    let len = parseInt(value.length)
    this.setData({
      currentWordNumber: len,//当前字数 
      textValue: value
    })
  },
  chooseImage: function(e) {
    var _this = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#00000",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            _this.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            _this.chooseWxImage('camera')
          }
        }
      }
    })
  },
  // 图片本地路径
  chooseWxImage: function (type) {
    var that = this;
    var imgsPaths = that.data.imgs;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        console.log(res.tempFilePaths[0]);
        that.upImgs(res.tempFilePaths[0], 0) //调用上传方法
      }
    }) 
  },
  upImgs: function (imgurl, index) {
    var _this = this;
    wx.uploadFile({
      url: 'https://hi.lengjishan.cn/automation/upload',
      filePath: imgurl,
      name: 'file',
      header: {
        'content-type': 'multipart/form-data',
        'openId': wx.getStorageSync('openid')
      },
      formData: {path: 'damage/pic'},
      success: function (res) {
        console.log(1,res.data)
        var data = JSON.parse(res.data)
        if(data.code == 200){
          console.log(2,data)
          _this.setData({
            
          })
        }else{
          wx.showToast({
            title: res.msg, //提示的内容,
            icon: "none", //图标,
            //duration: 1500, //延迟时间,
            mask: false, //显示透明蒙层，防止触摸穿透,
          });
          setTimeout(function() {
            wx.hideToast();
          }, 1500);
          return
        }
      }
    })
  },
  submit: function (e) {
    console.log('type',this.data.type)
    console.log('number',this.data.number)
    console.log('textValue',this.data.textValue)
    if(!this.data.type){
      wx.showToast({
        title: '请选择申请单位', //提示的内容,
        icon: "none", //图标,
        //duration: 1500, //延迟时间,
        mask: false, //显示透明蒙层，防止触摸穿透,
      });
      setTimeout(function() {
        wx.hideToast();
      }, 1500);
      return
    }
    if(this.data.currentWordNumber<10){
      wx.showToast({
        title: '申请原因最少10个字符', //提示的内容,
        icon: "none", //图标,
        //duration: 1500, //延迟时间,
        mask: false, //显示透明蒙层，防止触摸穿透,
      });
      setTimeout(function() {
        wx.hideToast();
      }, 1500);
      return
    }
    const _this = this
    util.request(getApp().api.goods.refund,{
      orderId: e.currentTarget.dataset.id,
      reason: this.data.textValue,
      refundNum: this.data.number,
      type: this.data.type
    },'post').then(
      function(res) {
        if (res.code == 200) {
          wx.navigateTo({
              url: '/pages/order-list/order-list?id=0'
          })
        } else {
          wx.showToast({
            title: res.msg, //提示的内容,
            icon: "none", //图标,
            //duration: 1500, //延迟时间,
            mask: false, //显示透明蒙层，防止触摸穿透,
          });
          setTimeout(function() {
            wx.hideToast();
          }, 1500);
          return
        }
      }
    );
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})